package com.zhs.gccs.view.taskMap;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.drawerlayout.widget.DrawerLayout;

import com.hjq.shape.view.ShapeTextView;
import com.zhs.gccs.R;
import com.zhs.gccs.activity.LoginActivity;
import com.zhs.gccs.activity.app.AppVersionActivity;

public class LeftMenuView extends LinearLayout implements View.OnClickListener{
    private ShapeTextView tv_header;
    private TextView tv_user;
    private TextView tv_user_dept;
    private TextView tv_app_version;
    private TextView tv_exit;
    private Context context;
    private DrawerLayout drawerLayout;

    public LeftMenuView(@NonNull Context context) {
        super(context);
        init(context, null,0);
    }

    public LeftMenuView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs,0);
    }

    public LeftMenuView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs,defStyleAttr);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.draw_menu_layout, this, true);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomerLogo);
//        String title = typedArray.getString(R.styleable.CustomerInfoItem_preText);
//        typedArray.recycle();
        tv_header= view.findViewById(R.id.tv_header);
        tv_user = view.findViewById(R.id.tv_user);
        tv_user_dept = view.findViewById(R.id.tv_user_dept);
        tv_app_version = view.findViewById(R.id.tv_app_version);
        tv_app_version.setOnClickListener(this);
        tv_exit = view.findViewById(R.id.tv_exit);
        tv_exit.setOnClickListener(this);
    }
    public LeftMenuView setDrawerLayout(DrawerLayout drawerLayout) {
        this.drawerLayout = drawerLayout;
        return this;
    }


    public LeftMenuView setUserName(String userName) {
        if(TextUtils.isEmpty(userName)){
            tv_user.setText("");
            tv_header.setText("");
            return this;
        }
        tv_user.setText(userName);
        tv_header.setText(userName.substring(0,1));
        return this;
    }

    public LeftMenuView setUserDept(String dept) {
        if(TextUtils.isEmpty(dept)){
            tv_user_dept.setText("");
            return this;
        }
        tv_user_dept.setText(dept);
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_app_version:{
                Intent intent = new Intent(context, AppVersionActivity.class);
                context.startActivity(intent);
                closeDrawer();
                break;
            }
            case R.id.tv_exit:{
                Intent intent = new Intent(context, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(intent);
                closeDrawer();
                break;
            }
        }
    }

    private void closeDrawer() {
        if(this.drawerLayout!=null)this.drawerLayout.closeDrawer(Gravity.LEFT);
    }
}
